package com.lm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lm.pojo.Docter;
import com.lm.pojo.Organization;
import com.lm.pojo.SearchOrganization;
import com.lm.pojo.TeamManage;
import com.lm.service.OrgService;
import com.lm.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @version 1.0
 * @dscription:TODO
 * @author32477
 * @date 2025/9/20 23:22
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/org")
@Api(tags = "组织管理")
public class OrgController {
    @Autowired
    private OrgService orgSevice;

    /**
     * @description:
     * @param: org
     * @return: com.lm.util.ResultUtil<com.lm.pojo.Organization>
     * @author 32477
     * @date: 2025/9/21 0:08
     */
    @ApiOperation(value = "查询组织列表", notes = "查询组织列表", httpMethod = "POST")
    @PostMapping("/findList")
    public ResultUtil<IPage<Organization>> findList(@RequestBody SearchOrganization org){
        try {
            System.out.println(org.getPageSize()+","+org.getPageNo());
            //将数据包装到PageInfo对象中
            IPage<Organization> pageInfo = orgSevice.findList(org);
            return new ResultUtil<>(200,"查询成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500,"查询失败");
        }
    }

    /**
     * @description:添加 组织
     * @param: org
     * @return: com.lm.util.ResultUtil<java.lang.String>
     * @author 32477
     * @date: 2025/9/21 0:32
     */
    @ApiOperation(value = "添加组织", notes = "添加组织", httpMethod = "POST")
    @PostMapping("/addOrg")
    public ResultUtil<String> addOrg(@RequestBody Organization org){
        try {
            orgSevice.addOrg(org);
            return new ResultUtil<>(200,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500,"添加失败");
        }
    }
    /**
     * @description: 修改 组织
     * @param: orgId
     * @return: com.lm.util.ResultUtil<java.lang.String>
     * @author 32477
     * @date: 2025/9/21 0:41
     */
    @ApiOperation(value = "删除组织", notes = "删除组织", httpMethod = "DELETE")
    @DeleteMapping("/deleteOrg")
    public ResultUtil<String> deleteOrg(@RequestParam Integer orgId){
        try {
            orgSevice.deleteOrg(orgId);
            return new ResultUtil<>(200,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500,"删除失败");
        }
    }

    /** 
     * @description: 删除组织 
     * @param:
     * @return: com.lm.util.ResultUtil<java.lang.String> 
     * @author 32477
     * @date: 2025/9/21 1:01
     */ 
    @ApiOperation(value = "修改组织", notes = "修改组织", httpMethod = "PUT")
    @PutMapping("/updateOrg")
    public ResultUtil<String> updateOrg(@RequestBody Organization org){
        try {
            orgSevice.updateOrg(org);
            return new ResultUtil<>(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500,"修改失败");
        }
    }

    /**
     * @description:查询组织最大编号
     * @param:
     * @return: com.lm.util.ResultUtil<java.lang.Integer>
     * @author 32477
     * @date: 2025/9/21 21:36
     */
    @ApiOperation(value = "查询组织编号", notes = "查询组织编号", httpMethod = "GET")
    @GetMapping("/findLastNumber")
    public ResultUtil<Integer> findLastNumber(){
        try {
            Integer number =orgSevice.findLastNumber();
            return new ResultUtil<>(200,"查找成功",number);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500,"查找失败");
        }
    }
    /**
     * @description:  查询医生组织
     * @param:
     * @return: com.lm.util.ResultUtil<java.util.List<com.lm.pojo.Docter>>
     * @author 32477
     * @date: 2025/9/22 10:07
     */

    @ApiOperation(value = "查询组织医生列表", notes = "查询组织医生列表", httpMethod = "GET")
    @GetMapping("/findDocterList")
    public ResultUtil<List<Docter>>findDocterList(){
        try {
            List<Docter> docterList = orgSevice.findDocterList();
            return new ResultUtil<>(200,"查询成功",docterList);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500,"查询失败");
        }
    }

    /**
     * @description:
     * @param: orgId
    orgState
     * @return: com.lm.util.ResultUtil<java.lang.String>
     * @author 32477
     * @date: 2025/9/22 10:09
     */
    @ApiOperation(value = "修改组织状态", notes = "修改组织状态", httpMethod = "GET")
    @GetMapping("/changeState")
    public ResultUtil<String> changeState(@RequestParam(value = "orgId")Integer orgId,@RequestParam(value = "orgState")Integer orgState){
        try {
            orgSevice.changeState(orgId,orgState);
            return new ResultUtil<>(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500,"修改失败");
        }
    }

    @ApiOperation(value = "查询组织编号和名称", notes = "查询组织编号和名称", httpMethod = "GET")
    @GetMapping("/findIdAndName")
    public ResultUtil<List<Organization>> findIdAndName(){
        try {
            List<Organization> list = orgSevice.findIdAndName();
            return new ResultUtil<>(200,"查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500,"查询失败");
        }
    }
    @ApiOperation(value = "查询组织医生团队信息", notes = "查询组织医生团队信息", httpMethod = "GET")
    @GetMapping("/findTeamByOrgId")
    public ResultUtil<List<TeamManage>> findTeamByOrgId(@Param("orgId") Integer orgId){
        try {
            List<TeamManage> list = orgSevice.findTeamList(orgId);
            return new ResultUtil<>(200,"查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<>(500,"查询失败");
        }
    }
}
